package com.unionPay.data;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Xml;
import android.util.Xml.Encoding;

public class SaxParser {
	public static String parserTag;

	public static final DefaultHandler parser(String xmlPath,
			final DefaultHandler deHandler) {
		boolean isCorrect = (xmlPath != null && !xmlPath.trim().equals("") && deHandler != null);
		if (isCorrect) {

			try {
				InputStream in = new FileInputStream(xmlPath);
				Xml.parse(in, Encoding.UTF_8, deHandler);
				in.close();
				in = null;
				xmlPath = null;
			} catch (IOException e) {
				e.printStackTrace();
			} catch (SAXException e) {
				e.printStackTrace();
			}
		}
		return deHandler;
	}

	public static final DefaultHandler parser(InputStream in,
			final DefaultHandler deHandler) {
		boolean isCorrect = (in != null && deHandler != null);
		if (isCorrect) {
			try {
				Xml.parse(in, Encoding.UTF_8, deHandler);
				in.close();
				in = null;
			} catch (IOException e) {
				e.printStackTrace();
			} catch (SAXException e) {
				e.printStackTrace();
			}
		}
		return deHandler;
	}

	public static final DefaultHandler parserxml(String xml,
			final DefaultHandler deHandler) {
		boolean isCorrect = (xml != null && deHandler != null);
		if (isCorrect) {
			try {
				Xml.parse(xml, deHandler);
				xml = null;
			} catch (SAXException e) {
				e.printStackTrace();
			}
		}
		return deHandler;
	}

}
